package com.wentao.mall.api

import com.wentao.mall.dao.CustomerDao
import com.wentao.mall.entity.Customer
import io.swagger.v3.oas.annotations.Operation
import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestMethod.GET
import org.springframework.web.bind.annotation.RequestMethod.POST
import org.springframework.web.bind.annotation.ResponseBody

@Controller
class BaseController(
    val customerDao: CustomerDao) {

    @RequestMapping(value = ["/all"], method = [GET])
    @ResponseBody

    fun all(): MutableList<Customer> {
        val all = customerDao.findAll()
        return all
    }

    @Operation(summary = "增加")
    @RequestMapping("add", method = [GET, POST])
    @ResponseBody
    fun add(): Customer {
        val customer = Customer(id = null, name = "xz")
        val save = customerDao.save(customer)
        return save;
    }

//    @GetMapping(value = [
//        "/"
//        ,"welcome",
//        "userList/**",
//        "buyerOrderList/**",
//        "invoiceList/**",
//        "merchantList/**",
//        "productList/**",
//        "pointsHistory/**",
//        "user/login/**",
//        "user/login"
//    ])
//    fun index(): String {
//        return "index.html"
//    }

}

data class Result(
    val success: Boolean = true,
    val errMsg: String? = null,
    val data: Any? = null
) {

    companion object {
        fun success(): Result {
            return Result(success = true)
        }

        fun success(data: Any): Result {
            return Result(data = data)
        }

        fun error(errMsg: String): Result {
            return Result(success = false, errMsg = errMsg)
        }

        fun errorUserNotFound(): Result {
            return Result(success = false, errMsg = "user not found")
        }
    }
}